package edu.cicese.alz;

import edu.cicese.alz.reports.NFCPane;
import edu.cicese.alz.swing.SwingUtil;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 24/07/12
 * Time: 12:53 PM
 */
public class FrameNFCTags extends SwingComponent {

	private PaneNFCTags paneNFCTags;
	private JButton btnSave, btnCancel;

	private NFCPane nfcReportPane;
	private boolean modal;

	public static void main(String[] a) {
		SwingUtil.setUIManager();
		FrameNFCTags frameNFCTags = new FrameNFCTags();
		frameNFCTags.setVisible(true);
		frameNFCTags.flash();
	}

	public void flash() {
		try {
			Thread.sleep(5000);
			Toolkit.getDefaultToolkit().beep();
		} catch (InterruptedException e) {
		}
	}

	public FrameNFCTags() {
		super(600, 600, "NFC Tags");

		initComponents();
	}

	private void initComponents() {
		JPanel pContent = new JPanel(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();

		// Row 1
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc.insets = new Insets(5, 5, 0, 5);
		pContent.add(paneNFCTags = new PaneNFCTags(this), gbc);

		// Row 2
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.weightx = 0;
		gbc.weighty = 0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(5, 5, 5, 5);
		pContent.add(createBoxButtons(), gbc);

		pContent.setBackground(SwingUtil.cBackground);
		setContentPane(pContent);
	}

	private Box createBoxButtons() {
		JButton btnOk = createButtonBackground("OK", 71, 22, null, "bg");
		btnSave = createButtonBackground("Save", 71, 22, null, "bg");
		btnCancel = createButtonBackground("Cancel", 71, 22, null, "bg");

		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				paneNFCTags.actionSaveChanges(true);
				if (modal) {
					nfcReportPane.reloadProfiles();
				}
			}
		});
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				paneNFCTags.actionCancelChanges();
				if (modal) {
					nfcReportPane.reloadProfiles();
				}
			}
		});
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				paneNFCTags.actionSaveChanges(false);
			}
		});

		Box boxButtons = Box.createHorizontalBox();
		boxButtons.add(Box.createHorizontalGlue());
		boxButtons.add(btnOk);
		boxButtons.add(Box.createHorizontalStrut(7));
		boxButtons.add(btnSave);
		boxButtons.add(Box.createHorizontalStrut(7));
		boxButtons.add(btnCancel);

		return boxButtons;
	}

	protected void enableButtons(boolean enable) {
		btnSave.setEnabled(enable);
//		btnCancel.setEnabled(enable);
	}

	public void setNFCPane(NFCPane nfcReportPane) {
		this.nfcReportPane = nfcReportPane;
	}

	public void reloadProfiles() {
		nfcReportPane.reloadProfiles();
	}

	public void setModal(boolean modal) {
		this.modal = modal;
	}

	public boolean isModal() {
		return modal;
	}
}
